package pers.fqq.dlfd.student.util;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.github.javafaker.Faker;
import pers.fqq.dlfd.student.entity.StudentEntity;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 模型生成工厂
 *
 * @author szhu
 */
public class ModelFactory {
    /**
     * faker 指定汉语，默认英语
     */
    private static Faker FAKER = new Faker(Locale.CHINA);


    /**
     * 随机生成一定数量学生
     *
     * @param number 数量
     * @return 学生
     */
    public static List<StudentEntity> listStudentList(final int number) {
        return Stream.generate(() -> new StudentEntity(FAKER.idNumber().valid(), FAKER.name().fullName(), FAKER.number().digits(10), String.valueOf(FAKER.number().numberBetween(0, 1))
        )).limit(number).collect(Collectors.toList());
    }

    /**
     * main函数
     */
    public static void main(String[] args) throws Exception {
//        listStudentList(100).forEach(System.out::println);
//        List<StudentEntity> list = ModelFactory.listStudentList(100);
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("学生表","学生信息"),
//                StudentEntity.class, list);
//        TemplateExportParams params = new TemplateExportParams("emp.xls");
//        Map<String, Object> data = new HashMap<String, Object>();
//        data.put("list", list);//导出list集合
//        Workbook workbook = ExcelExportUtil.exportExcel(params, data);
        //保存数据

//        FileOutputStream fos = new FileOutputStream("emp.xls");
//        workbook.write(fos);
//        fos.close();
//
//        //导入参数
        ImportParams params = new ImportParams();
        params.setTitleRows(1);     //导入数据的时候排除标题行
        params.setHeadRows(1);      //导入数据的时候排除表头行

        List<Map<String, String>> list = ExcelImportUtil.importExcel(
                new File("1.xlsx"), Map.class, params);

        Map<String, String> stu = new HashMap<>();
        stu.put("李婧祎", "09010005");
        stu.put("王籽钰", "09010011");
        stu.put("131003201507023448", "刘卓妍");
        stu.put("刘卓妍", "131003201507023448");
        stu.put("朱笑雨", "131003201511134415");
        stu.put("131003201511134415", "朱笑雨");
        stu.put("万苡菲", "131003201601233425");
        stu.put("131003201601233425", "万苡菲");
        stu.put("武沐阳", "131003201603134439");
        stu.put("131003201603134439", "武沐阳");
        stu.put("孟晧柠", "131003201603274028");
        stu.put("131003201603274028", "孟晧柠");
        stu.put("陈盛岳", "131003201607312415");
        stu.put("131003201607312415", "陈盛岳");
        stu.put("李宇曦", "131003201608094245");
        stu.put("131003201608094245", "李宇曦");
        stu.put("常唯惜", "131003201609074422");
        stu.put("131003201609074422", "常唯惜");
        stu.put("周芮含", "131003201610224424");
        stu.put("131003201610224424", "周芮含");
        stu.put("马维灏", "131003201704085429");
        stu.put("131003201704085429", "马维灏");
        stu.put("王博涵", "23118120160618603X");
        stu.put("23118120160618603X", "王博涵");

        String name = "";
        String code = "";
        String signdate = "";
        String status = "";
        int i = 0;
        for (Map<String, String> a : list) {
            for (Map.Entry<String, String> entry : a.entrySet()) {
                if (entry.getKey().equals("序号")) {
                    continue;
                }
                if (entry.getKey().equals("姓名")) {
                    name = entry.getValue();
                    code = stu.get(name);
                    continue;
                }


                signdate = "2020-01-" + (entry.getKey().substring(0, entry.getKey().indexOf(".")).length() == 1 ? "0" + entry.getKey().substring(0, entry.getKey().indexOf(".")) : entry.getKey().substring(0, entry.getKey().indexOf(".")));
                if (entry.getValue().equals("√")) {
                    status = "1";
                } else {
                    status = "2";
                }
                System.out.println("INSERT INTO education.sign ( code,  name,signdate, status, classid, gradeid, schoolid, description, classname, gradename) " +
                        "VALUES ('" + code + "', '" + name + "', '" + signdate + "'," + status + ",188, 29, 6, '', '海棠班', '2020年幼儿班');");

                i++;
            }

        }
        System.out.println(i);
    }

}